
requirejs(['jquery', 'Util', 'bootstrap', 'metisMenu', 'slimScroll', 'inspinia', 'datePicker', 'dateRangePicker', 'fooTable', 'jQtmpl'], function ($, Util) {

    $(function () {

        /**
         * datepicker汉化
         */
        $.fn.datepicker.dates['zh-CN'] = {
            days: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"],
            daysShort: ["周日", "周一", "周二", "周三", "周四", "周五", "周六", "周日"],
            daysMin: ["日", "一", "二", "三", "四", "五", "六", "日"],
            months: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
            monthsShort: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
            today: "今日",
            suffix: [],
            meridiem: []
        };

        // 门店下拉框选择
        $('#shop-chosen').on('change', function () {
            var $node = $(this);
            $.post('/console/ajaxLoadBranche', {
                shop_id: $node.val()
            }, function (data) {
                if (data.length) {
                    // 有分店
                    $('#branche-not-empty').empty();
                    $('#branche-chosen-tmpl').tmpl(data).appendTo($('#branche-not-empty'));
                    var branche_id = $('#branche-id').val();
                    console.log(branche_id);
                    $('#branche-not-empty option').each(function (i, node) {
                        if ($(node).attr('value') == branche_id) {
                            console.log(node);
                            $(node).get(0).selected = true;
                        }
                    });
                    $('#branche-not-empty').show().siblings($('#branche-empty').hide());
                    $('#branche-not-empty').on('change', function () {
                        $('#branche-id').val($(this).val());
                        $('#address-detail').html($(this).find('option:selected').attr('data-phone') + '&emsp;' + $(this).find('option:selected').attr('data-address'));
                    }).trigger('change');
                } else {
                    // 无分店
                    $('#branche-id').val(0);
                    $('#address-detail').html($('#shop-chosen').find('option:selected').attr('data-phone') + '&emsp;' + $('#shop-chosen').find('option:selected').attr('data-address'));
                    $('#branche-empty').show().siblings($('#branche-not-empty').hide());
                }
            });
        }).trigger('change');

        // datepicker初始化
        $('#datepicker').datepicker({
            language: 'zh-CN',
            format: 'yyyy-mm-dd',
            keyboardNavigation: false,
            forceParse: false,
            autoclose: true,
            todayHighlight: true
        });

        // 保存按钮点击
        $('#save-btn').on('click', function () {

            // 表单验证
            var nodeList = ['coupon-info', 'coupon-rule', 'coupon-count', 'coupon-start', 'coupon-end'];
            var flag = 0;
            var branche_id = 0;

            if ($('#shop-chosen').val()) {
                $.each(nodeList, function (i, item) {
                    if ($('#' + item).val() == '') {
                        $('#' + item).parent($('div.form-group')).addClass('has-error');
                        flag++;
                    }
                });
            } else {
                flag = 100;
            }

            if (flag) {
                return false;
            } else {
                if ($('#branche-empty').is(':hidden')) {
                    branche_id = $('#branche-not-empty').val();
                }

                $.post('/console/ajaxEditCoupon', {
                    id: $('#coupon-id').val(),
                    shop_id: $('#shop-chosen').val(),
                    branche_id: branche_id,
                    info: $('#coupon-info').val(),
                    rule: $('#coupon-rule').val(),
                    count: $('#coupon-count').val(),
                    start: $('#coupon-start').val(),
                    end: $('#coupon-end').val()
                }, function (data) {
                    if (data.ret_msg === 'success') {
                        Util.Alert('保存成功!');
                        window.location.href = '/console/coupon';
                    } else {
                        Util.Alert('保存失败!', true);
                    }
                });
            }
        });



    });

});